package com.rg.servlet;

import com.rg.model.GoodsModel;
import com.rg.service.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/checkgoodsStock")
public class checkgoodsStock extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int goodsId=Integer.parseInt(request.getParameter("goodsId"));
        int addnum=Integer.parseInt(request.getParameter("addnum"));
        System.out.print("-----------");
        System.out.print(addnum);
        GoodsService goodsService=new GoodsService();
        GoodsModel goods=goodsService.selectOneGoodsBygoodsId(goodsId);
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String jsonstr=(goods.getGoodsStock()>addnum)?"{\"code\":200,\"msg\":\"success\"}":"{\"code\":400,\"msg\":\"fail\"}";
        PrintWriter out=response.getWriter();
        out.println(jsonstr);
        out.flush();
        out.close();
    }
}
